package orion.cricScoreCard.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {

	SQLiteOpenHelper soh;

	public DBAdapter(Context context) {
		soh = new DBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	//Database Name
	private static final String DATABASE_NAME = "CricScoreCard";
	private static final int DATABASE_VERSION = 1;

	//Table Name
	private static final String TABLE_MATCH = "match_details";
	private static final String TABLE_BATTING = "batting_details";
	private static final String TABLE_BOWLING = "bowling_details";
	
	//Field Name
	public static final String MATCH_ID = "match_id";
	public static final String TEAM1 = "_team1";
	public static final String TEAM2 = "_team2";
	public static final String TOSS = "_toss";
	public static final String RESULT = "_result";
	public static final String TEAMID = "_teamID";
	public static final String OVERS = "_overs";
	public static final String BATSMAN = "_batsman";
	public static final String OUT = "_out";
	public static final String RUN = "_run";
	public static final String BALL = "_ball";
	public static final String FOUR = "_four";
	public static final String SIX = "_six";
	public static final String BOWLER = "_bowler";
	public static final String WICKET = "_wicket";
	public static final String EXTRA = "_extra";

	class DBHelper extends SQLiteOpenHelper {

		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		
		//Creating Tables
		@Override
		public void onCreate(SQLiteDatabase db) {
			String CREATE_TABLE_MATCH = "CREATE TABLE " + TABLE_MATCH + "("
					+ MATCH_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TEAM1
					+ " TEXT NOT NULL, " + TEAM2 + " TEXT NOT NULL, " + OVERS
					+ " TEXT NOT NULL, " + TOSS + " TEXT NOT NULL, " + RESULT
					+ " TEXT NOT NULL );";
			db.execSQL(CREATE_TABLE_MATCH);

			String CREATE_TABLE_BATTING = "CREATE TABLE " + TABLE_BATTING + "("
					+ TEAMID + " TEXT NOT NULL, " + BATSMAN
					+ " TEXT NOT NULL, " + OUT + " TEXT NOT NULL, " + RUN
					+ " TEXT NOT NULL, " + BALL + " TEXT NOT NULL, " + FOUR
					+ " TEXT NOT NULL, " + SIX + " TEXT NOT NULL );";
			db.execSQL(CREATE_TABLE_BATTING);

			String CREATE_TABLE_BOWLING = "CREATE TABLE " + TABLE_BOWLING + "("
					+ TEAMID + " TEXT NOT NULL, " + BOWLER + " TEXT NOT NULL, "
					+ OVERS + " TEXT NOT NULL, " + RUN + " TEXT NOT NULL, "
					+ WICKET + " TEXT NOT NULL, " + EXTRA + " TEXT NOT NULL);";
			db.execSQL(CREATE_TABLE_BOWLING);
		}

		
		//On change of Database version
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_MATCH);
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_BATTING);
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_BOWLING);
			onCreate(db);
		}

	}

	SQLiteDatabase db;

	
	//insert to Match table
	public long insert_match(String team1, String team2, String overs,
			String toss, String result) {
		db = soh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(TEAM1, team1);
		cv.put(TEAM2, team2);
		cv.put(OVERS, overs);
		cv.put(TOSS, toss);
		cv.put(RESULT, result);
		return db.insert(TABLE_MATCH, null, cv);
	}

	//insert to Batting Table
	public long insert_batting(String teamID, String batsman, String out,
			String run, String ball, String four, String six) {
		db = soh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(TEAMID, teamID);
		cv.put(BATSMAN, batsman);
		cv.put(OUT, out);
		cv.put(RUN, run);
		cv.put(BALL, ball);
		cv.put(FOUR, four);
		cv.put(SIX, six);
		return db.insert(TABLE_BATTING, null, cv);
	}

	//insert to Bowling table
	public long insert_bowling(String teamID, String bowler, String run,
			String over, String wicket, String extras) {
		db = soh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(TEAMID, teamID);
		cv.put(BOWLER, bowler);
		cv.put(RUN, run);
		cv.put(OVERS, over);
		cv.put(WICKET, wicket);
		cv.put(EXTRA, extras);
		return db.insert(TABLE_BOWLING, null, cv);
	}

	//View data in Match table
	public String match_viewAll() {
		String str = "";
		db = soh.getReadableDatabase();
		Cursor cur = db.query(TABLE_MATCH, null, null, null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str = str + cur.getString(1) + "\t" + cur.getString(2) + "\n";
		}
		return str;
	}

	//View data in Batting table
	public String batting_viewAll() {
		String str = "";
		db = soh.getReadableDatabase();
		Cursor cur = db
				.query(TABLE_BATTING, null, null, null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str = str + cur.getString(1) + "\t" + cur.getString(2) + "\n";
		}
		return str;
	}

	//View data in Bowling table
	public String bowling_viewAll() {
		String str = "";
		db = soh.getReadableDatabase();
		Cursor cur = db
				.query(TABLE_BOWLING, null, null, null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str = str + cur.getString(1) + "\t" + cur.getString(2) + "\n";
		}
		return str;
	}

	//Search in Match table
	public String[] searchMatchData(String name) {
		String str[] = new String[2];
		db = soh.getReadableDatabase();
		Cursor cur = db.query(TABLE_MATCH, null, MATCH_ID + "='" + name + "'",
				null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str[0] = cur.getString(1) + "\n";
			str[1] = cur.getString(2) + "\n";
		}
		return str;
	}

	//Search in Batting table
	public String[] searchBattingData(String name) {
		String str[] = new String[2];
		db = soh.getReadableDatabase();
		Cursor cur = db.query(TABLE_BATTING, null, TEAMID + "='" + name + "'",
				null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str[0] = cur.getString(1) + "\n";
			str[1] = cur.getString(2) + "\n";
		}
		return str;
	}

	//Search in Bowling table
	public String[] searchBowlingData(String name) {
		String str[] = new String[2];
		db = soh.getReadableDatabase();
		Cursor cur = db.query(TABLE_BOWLING, null, TEAMID + "='" + name + "'",
				null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			str[0] = cur.getString(1) + "\n";
			str[1] = cur.getString(2) + "\n";
		}
		return str;
	}

	//Closing table
	public void closeDB() {
		soh.close();

	}

}
